// $URL: http://mingw-lib.googlecode.com/svn/trunk/working/avcodec_to_widget_8/widget.h $
// $Rev: 298 $
// $Author: akio.miyoshi $
// $Date:: 2010-07-28 14:12:27 +0000#$

#ifndef WIDGET_H
#define WIDGET_H

#include "t_av_packet.h"
#include "my_SDL_compat.h"

#include "misc.h"
#include "t_httpbackend.h"
#include "t_generator.h"
#include "t_box_player.h"

class Widget;
class T_ScreenWidget : public QWidget
{
public:
    QSize m_size;
    SDL_Surface *screen;
    class Widget *m_widget;
    QImage m_image;

    //explicit T_ScreenWidget(QWidget *parent = 0)
    explicit T_ScreenWidget(Widget *parent)
        : QWidget((QWidget *)parent)
        , m_size(QSize(200, 100))
    {
        m_widget = parent;
        //QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        sizePolicy.setHorizontalStretch(1);
        sizePolicy.setVerticalStretch(1);
        sizePolicy.setHeightForWidth(this->sizePolicy().hasHeightForWidth());
        this->setSizePolicy(sizePolicy);
    }
    virtual void setScreenSize(QSize a_size)
    {
        m_size = a_size;
    }
    virtual QSize sizeHint () const
    {
        return m_size;
    }
    virtual void paintEvent(QPaintEvent *e);
};

class T_Packet_Thread : public QThread
{
protected:
    T_Box_Player_Context *m_player_ctx;
    Widget *m_widget;
    bool m_closed;
    bool m_seek;
    qint64 m_max_timing;
    qint64 m_seek_timing;
public:
    //explicit T_Packet_Thread(T_Box_Player_Context *a_player_ctx)
    explicit T_Packet_Thread(T_Box_Player_Context *a_player_ctx, Widget *a_widget)
        : m_player_ctx(a_player_ctx)
        , m_widget(a_widget)
        , m_closed(false)
        , m_seek(false)
        , m_max_timing(0)
        , m_seek_timing(0)
    {
    }
    void close()
    {
        m_closed = true;
    }
    virtual void run();
};

namespace Ui {
    class Widget;
}
class T_Box_Input;
class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
public:
    T_ScreenWidget *screenWidget;

    T_Box_Input *m_media_input;
    T_Box_Player_Context *m_player_ctx;
    T_Packet_Thread *m_packet_thread;
    QString m_media_name;
    QTimer m_video_timer;
    int m_drop_count;
    T_FPS m_fps1;
    T_FPS m_fps2;
    T_FPS m_fps_download;


    QMutex m_frame_mutex;
    T_RecursiveMutex m_packet_mutex;

    SDL_Window *window_id;

#if 0x0
    T_Generator *m_generator;
#endif
    QAudioDeviceInfo m_device;
    QAudioFormat m_format;

    T_NetworkAccessManager m_nam;
    ////QNetworkAccessManager m_nam2;
    T_StopWatch m_boost_timer;

    QDateTime m_suspend_time;
    QString m_suspend_pos;

    T_Box_Input *get_input_file(const QString &a_filename);
    T_Box_Input *get_input_youtube(const QString &a_video_id);
    T_Box_Input *get_input_youtube2(const QString &a_video_id, int a_fmt);
    void suspend_time(const QString & a_suspend_pos);
    void resume_time();
    void show_status();
    void wait_thread(QThread *a_thread)
    {
        if(!a_thread) return;
        if(qApp->thread() != QThread::currentThread())
        {
            a_thread->wait();
            return;
        }
        while(a_thread->isRunning())
        {
            //qDebug() << "(a_thread->isRunning())";
            qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
            ::Sleep(10);
        }
    }

public slots:
    void onTimerTimeout();
    void on_progress_clicked(int value);

private slots:
    void on_pushButton_2_clicked();
    void on_pushButton_clicked();
};

#endif // WIDGET_H
